home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / strings.swg / 0118_String Input-Output.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-09-04  |  16.9 KB  |  530 lines

  1. { *** Handles string in/output and various conversion routines
  2.   ***
  3. }
  4.  
  5. Unit StrIO;
  6.  
  7.  
  8.  
  9. INTERFACE
  10.  
  11. Uses Vars;
  12.  
  13.      FUNCTION StatusBar(total, amt, barlength: longint): St80;
  14.      {FUNCTION StatusBar(total, amt : longint): St80;}
  15.      FUNCTION ITOA(i: longint): St40;
  16.      FUNCTION ATOI(s: St40): LongInt;
  17.      FUNCTION UpCase(c: Char): Char;
  18.      FUNCTION UCase(s: String): String;
  19.      FUNCTION RepStr(Times: Byte; Which: Char): String;
  20.      FUNCTION Strip_Path(Fullfilename: String): String;
  21.      FUNCTION Leading_Zero(Number: String; Digits: Byte): String;
  22.      FUNCTION Read_Str(StrLen     : Byte;
  23.                        InputFg,
  24.                        InputBg    : Integer;
  25.                        Hidden,
  26.                        Spaces     : Char;
  27.                        SpinWanted,
  28.                        Display,
  29.                        Upper,
  30.                        OnlyNumbers,
  31.                        AutoReturn : Boolean;
  32.                        Default    : String): String;
  33.      PROCEDURE Flush_Keyboard_Buffer;
  34.      FUNCTION Right_Pad(s: String; MaxLength: Word): String;
  35.      FUNCTION Right_Strip(s: String): String;
  36.      FUNCTION Right_Justify(s: String; sl: Byte): String;
  37.      FUNCTION CommaNum (I : LongInt): String;
  38.      FUNCTION Strip_Filename(S: String): String;
  39.  
  40.  
  41. CONST
  42.      Str_Yes  : String = 'Yes';
  43.      Str_No   : String = 'No';
  44.  
  45. IMPLEMENTATION
  46.  
  47. Uses Crt;
  48.  
  49. FUNCTION CharStr(HowMuch: Byte; WithWhatChar: Char): String;
  50. {
  51.  *** fills charStr with withwhatchar to the howmuch
  52.  ***
  53. }
  54.          Var
  55.             j       : Integer;
  56.             TempStr : St80;
  57.  
  58.          Begin
  59.               TempStr := '';
  60.               For J := 1 To HowMuch Do
  61.                   Insert(WithWhatChar, TempStr, J);
  62.               CharStr := TempStr;
  63.          End;
  64.  
  65.  
  66.  
  67.  
  68. FUNCTION StatusBar(total, amt, barlength: longint): St80;
  69. {         Const
  70.               BarLength = 30;}
  71.  
  72.          Var
  73.             a,
  74.             b,
  75.             c,
  76.             d       : longint;
  77.             sD      : String; {for conversion}
  78.             percent : real;
  79.             st      : string;
  80.  
  81.          Begin
  82.               If (total = 0) OR (amt = 0) Then
  83.                  Begin
  84.                       StatusBar := '';
  85.                       Exit;
  86.                  End;
  87.               If (Amt > Total) Then
  88.                  amt := total;
  89.               Percent := Amt / Total * (Barlength * 10);
  90.               a := trunc(percent);
  91.               b := a div 10;
  92.               c := 1;
  93.               percent := amt / total * 100;
  94.               d := trunc(percent);
  95.               Str(d, sD);
  96.               st := ' (' + sD + '%)';
  97.               StatusBar := CharStr(b * c, #219) + CharStr(Barlength - (b * c), #176) + st;
  98.          End;
  99.  
  100.  
  101.  
  102.  
  103. FUNCTION ITOA(i: longint): St40;
  104. {
  105.  *** Converts integers into alphanumericals or strings
  106.  ***
  107. }
  108.          Var
  109.             stTemp: St20;
  110.  
  111.          Begin
  112.               Str(i, stTemp);
  113.               ITOA := stTemp;
  114.          End;
  115.  
  116.  
  117. FUNCTION ATOI(s: St40): LongInt;
  118. {
  119.  *** Converts a string into a integer/real
  120.  ***
  121. }
  122.          Var
  123.             Code: Integer;
  124.             lTemp: LongInt;
  125.             rTemp: Real;
  126.  
  127.          Begin
  128.               Val(s, rTemp, Code);
  129.               If (Code <> 0) Then
  130.                  rTemp := 0;
  131.               lTemp := Trunc(rTemp);
  132.               ATOI := lTemp;
  133.          End;
  134.  
  135. FUNCTION UpCase(C: Char): Char; Assembler; { will replace TP's built-in upcase }
  136.          ASM
  137.             MOV DL, C
  138.             MOV AX, $6520
  139.             INT $21
  140.             MOV AL, DL           { function result in AL                 }
  141.          END;
  142.  
  143.  
  144. FUNCTION UCase(s: String): String;
  145. {
  146.  *** Converts any string(s) into upper case letters
  147.  ***
  148. }
  149.          Var
  150.             J : Integer;
  151.  
  152.          Begin
  153.               For J := 1 to Length(s) Do
  154.                   s[J] := StrIo.UpCase(s[J]);
  155.               UCase := S;
  156.          End;
  157.  
  158.  
  159. FUNCTION RepStr(Times: Byte; Which: Char): String;
  160.          Var
  161.             J        : Byte;
  162.             tString  : String;
  163.  
  164.          Begin
  165.               tString := '';
  166.               For J := 1 To Times Do
  167.                   tString := tString + Which;
  168.               RepStr := tString;
  169.          End;
  170.  
  171.  
  172. FUNCTION Strip_Path(Fullfilename: String): String;
  173.          Var
  174.             tString: String;
  175.  
  176.          Begin
  177.               tString := FullFilename;
  178.               While (Pos('\', tString) <> 0) Do
  179.                     Delete(tString, 1, Pos('\', tString));
  180.               Strip_Path := tString;
  181.          End;
  182.  
  183.  
  184. {
  185.  Makes sure that NUMBER is DIGITS digits.  Ie if DIGITS = 10 and NUMBER = 29
  186.  the result is 0000000029, 10 DIGITS :) Simple hugh?
  187. }
  188. FUNCTION Leading_Zero(Number: String; Digits: Byte): String;
  189.          Var
  190.             tString   : String;             {temporary zero holding spot}
  191.             NeedZeros : Integer;            {Number of zeros needed}
  192.             J         : Byte;               {for the FOR-LOOP}
  193.  
  194.          Begin
  195.               tString := '';
  196.               NeedZeros := Digits - Length(Number);
  197.               If (NeedZeros > 0) Then
  198.                  Begin
  199.                       for J := 1 TO NeedZeros Do
  200.                           tString := tString + '0';
  201.                       tString := tString + Number;
  202.                  End
  203.               Else
  204.                   tString := Number;
  205.  
  206.               Leading_Zero := tString;
  207.          End;
  208.  
  209.  
  210. FUNCTION Read_Str(StrLen     : Byte;
  211.                   InputFg,
  212.                   InputBg    : Integer;
  213.                   Hidden,
  214.                   Spaces     : Char;
  215.                   SpinWanted,
  216.                   Display,
  217.                   Upper,
  218.                   OnlyNumbers,
  219.                   AutoReturn : Boolean;
  220.                   Default    : String): String;
  221. {
  222.  *** Gets string from local/remote
  223.      StrLen - String length
  224.      InputFg - Foreground for input
  225.      InputBg - Background for input
  226.      Hidden - character to display instead of entered characters or #0
  227.      Spaces - Character to display where nothing is written.
  228.      Display - Display output
  229.      Upper - force upper case
  230.      OnlyNumbers - Characters between 0-9 are allowed, nothing else
  231.      AutoReturn - Wheter to hig enter automatically after STRLENth character
  232.      SpinWanted - Wheter or not to spin a character
  233.      Default - Text displayed as if user/modem typed it in.
  234.  ***
  235. }
  236.          Var
  237.             ChIn    : Char;         {character read in}
  238.             StrCount: Integer;      {current location in string}
  239.             J       : Integer;      {used in For-loop combo}
  240.             TempStr : String;       {temporary string}
  241.             OldX,
  242.             OldY,
  243.             OldFg,
  244.             OldBg    : Word;         {save coordinates}
  245.             SpinCount: Byte;
  246.  
  247.          Const
  248.               Spin   : Array [1..4] Of Char = ('|', '/', '-', '\');
  249.  
  250.          Begin
  251.               TempStr := '';
  252.               ChIn := #0;
  253.               StrCount := 0;
  254.               SpinCount := 0;
  255.  
  256.               if Default <> #0 Then
  257.                  Begin
  258.                       TempStr := Default;
  259.                       StrCount := Length(TempStr);
  260.                  End;
  261.  
  262.               If Display Then
  263.                 Begin
  264.                      OldX := WhereX;
  265.                      OldY := WhereY;
  266.                      OldFg := TextAttr MOD 16;
  267.                      OldBg := TextAttr SHR 4;
  268.                      TextColor(InputFg);  TextBackground(InputBg);
  269.                      if (Spaces < #32) Then
  270.                         Spaces := #32;
  271.                      For J := 1 to StrLen Do
  272.                          Write(Spaces);
  273.                      GotoXY(OldX, OldY);
  274.                      If (Default <> #0) Then
  275.                         Begin
  276.                              For J := 1 to Length(Default) Do
  277.                                  If (Hidden <> #0) Then
  278.                                     Write(Hidden)
  279.                                  Else
  280.                                      Write(Default[J]);
  281.                         End
  282.                 End;
  283.               Repeat
  284.                     Repeat
  285.                           If SpinWanted Then
  286.                              Begin
  287.                                   Inc(SpinCount);
  288.                                   If (SpinCount > 4) Then
  289.                                      SpinCount := 1;
  290.                                   Write(Spin[SpinCount]);
  291.                                   GotoXY(WhereX - 1, WhereY);
  292.                                   Delay(30);
  293.                                   Write(' ');
  294.                                   GotoXY(WhereX - 1, WhereY);
  295.                              End;
  296.                     Until Keypressed;
  297.                     ChIn := Readkey;
  298.  
  299.                     If (ChIn = #0) Then
  300.                        Exit;
  301.  
  302.                     If Upper then
  303.                        ChIn := Upcase(ChIn);
  304.  
  305.                     Case UpCase(ChIn) Of
  306.                         #19: Begin {left arrow}
  307.                                    If (StrCount > 1) Then
  308.                                       Begin
  309.                                            Dec(StrCount, 1);
  310.                                            If Display Then
  311.                                               GotoXY(WhereX - 1, WhereY);
  312.                                       End;
  313.  
  314.                              End;
  315.                          #4: Begin {right arrow}
  316.                                    If (StrCount < StrLen) Then
  317.                                       Begin
  318.                                            Inc(StrCount, 1);
  319.                                            Insert(#32, TempStr, StrCount);
  320.                                            If Display Then
  321.                                               GotoXY(WhereX + 1, WhereY);
  322.                                       End;
  323.                              End;
  324.                          #8: Begin
  325.                                   If (StrCount > 0) Then
  326.                                      Begin
  327.                                           Dec(StrCount, 1);
  328.                                           If Display Then
  329.                                             Begin
  330.                                                  GotoXY(WhereX - 1, WhereY);
  331.                                                  Write(Spaces);
  332.                                                  GotoXY(WhereX - 1, WhereY);
  333.                                             End;
  334.                                           Delete(TempStr, Length(TempStr), 1);
  335.                                      End;
  336.                                   ChIn := #0;
  337.                              End;
  338.                          #13: Begin
  339.                                    If Display Then
  340.                                       GotoXY(1, WhereY + 1);
  341.                               End;
  342.                        #32..#255: Begin
  343.                                        If (StrCount < StrLen) Then
  344.                                           Begin
  345.                                                If OnlyNumbers Then
  346.                                                   Begin
  347.                                                        Case ChIn Of
  348.                                                        '0'..'9', '.': Begin
  349.                                                                            Inc(StrCount);
  350.                                                                            Insert(ChIn, TempStr, StrCount);
  351.                                                                       End;
  352.                                                        Else {anything except numbers}
  353.                                                            ChIn := #0;
  354.                                                        End;
  355.                                                   End {if onlynumbers then}
  356.                                                Else
  357.                                                    Begin
  358.                                                        Inc(StrCount);
  359.                                                        Insert(ChIn, TempStr, StrCount);
  360.                                                    End;
  361.                                           End
  362.                                        Else
  363.                                            ChIn := #0;
  364.                                   End;
  365.                         Else
  366.                             ChIn := #0;
  367.                          End; {case}
  368.  
  369.                          If (StrCount = StrLen) Then
  370.                             Begin
  371.                                  If AutoReturn Then
  372.                                     Begin
  373.                                          ChIn := #13;
  374.                                          GotoXY(1, WhereY + 1);
  375.                                     End;
  376.                             End;
  377.  
  378.                          If Display AND (ChIn <> #0) Then
  379.                             if (Hidden > #32) Then {space or no pw}
  380.                                Write(Hidden)
  381.                             Else
  382.                                 Write(ChIn);
  383.               Until (ChIn = #13) OR (ChIn = #27);
  384.  
  385.               If Display Then
  386.                  Begin
  387.                       TextColor(OldFg);
  388.                       TextBackground(OldBg);
  389.                  End;
  390.  
  391.               Read_Str := TempStr;
  392.          End;
  393.  
  394.  
  395.  
  396. PROCEDURE Flush_Keyboard_Buffer;
  397.           Var
  398.              ChIn        : Char;        {for clearing the keyboard buffer}
  399.  
  400.           Begin
  401.                While Keypressed Do
  402.                      ChIn := ReadKey;
  403.           End;
  404.  
  405.  
  406. FUNCTION Right_Pad(s: String; MaxLength: Word): String;
  407.          Const
  408.               tString : String = '';
  409.               HowMany : Byte = 0;
  410.               J       : Byte = 0;
  411.  
  412.          Begin
  413.               J := 0;
  414.               HowMany := 0;
  415.               tString := '';
  416.  
  417.               {check for greater then number strings}
  418.               If (Length(s) > MaxLength) Then
  419.                  Begin
  420.                       tString := Copy(s, 1, MaxLength);
  421.                       Exit;
  422.                  End
  423.               Else
  424.                   Begin
  425.                        HowMany := (MaxLength - Length(s));
  426.                        Repeat
  427.                              Inc(J);
  428.                              tString := tString + #32;
  429.                        Until J >= HowMany;
  430.                        tString := s + tString;
  431.                   End;
  432.  
  433.               Right_Pad := tString;
  434.          End;
  435.  
  436. FUNCTION Right_Strip(s: String): String;
  437.          Var
  438.             StrLen,
  439.             Count        : Byte;
  440.  
  441.          Begin
  442.               StrLen := Length(s);
  443.               Count  := StrLen + 1;
  444.               Repeat
  445.                     Dec(Count);
  446.               Until (s[Count] <> #32);
  447.               Delete(s, Count + 1, StrLen - Count);
  448.               Right_Strip := S;
  449.          End;
  450.  
  451. FUNCTION Right_Justify(s: String; sl: Byte): String;
  452.          Var
  453.             tString2,
  454.             tString: String;
  455.             Where,
  456.             HowMuch: Byte;
  457.  
  458.          Begin
  459.               tString := '';
  460.               tString2 := '';
  461.               tString := s;
  462.               If Length(tString) > Sl Then
  463.                  Begin
  464.                       tString2 := Copy(tString, 1, Sl);
  465.                       Right_Justify := tString2;
  466.                       Exit;
  467.                  End;
  468.  
  469.               Where := 1;
  470.               Where := sl - Length(tString);
  471.  
  472.               FillChar(tString2, Where, #32);
  473.               Insert(tString, tString2, Where);
  474.               Delete(tString2, Where + Length(tString), Length(tString2) - (Where + Length(tString)) + 1);
  475.               Right_Justify := tString2;
  476.          End;
  477.  
  478. Function CommaNum (I : LongInt): String;
  479. Var
  480.     TmpString : String;
  481.     Counter, Tester : Byte;
  482. Begin
  483.   TmpString := '';
  484.   Counter   := 0;
  485.   Tester    := 0;
  486.   Str (i, TmpString);
  487.   For Counter := Length (TmpString) Downto 1 Do
  488.   Begin
  489.     Inc (Tester);
  490.     If Tester = 3 Then
  491.     Begin
  492.       Tester := 0;
  493.       Dec (Counter);
  494.       TmpString := Copy (TmpString, 1, Counter) + ','
  495.                  + Copy (TmpString, Counter + 1, Length (TmpString) );
  496.       Inc (Counter);
  497.     End;
  498.   End;
  499.   If TmpString[1] = ',' THEN DELETE(TmpString,1,1);
  500.   CommaNum := TmpString;
  501. End;
  502.  
  503.  
  504. {Returns the path from C:\BLOB\SHOOT\DIS.THD would give you C:\BLOB\SHOOT}
  505. FUNCTION Strip_Filename(S: String): String;
  506.          Var
  507.             SlashPos  : Byte;
  508.             tString   : String;
  509.  
  510.          Begin
  511.               tString := '';
  512.  
  513.               SlashPos := Pos('\', S);
  514.               If SlashPos = 0 Then
  515.                  Begin
  516.                       Strip_Filename := '';
  517.                       Exit;
  518.                  End;
  519.  
  520.               Repeat
  521.                     SlashPos := Pos('\', S);
  522.                     tString := tString + Copy(S, 1, SlashPos);
  523.                     Delete(s, 1, SlashPos);
  524.               Until SlashPos = 0;
  525.               Strip_FIlename := tString;
  526.          End;
  527.  
  528.  
  529. BEGIN
  530. END.